<?php

require_once ('system\core\Model.php');
require_once ("application/controllers/data/userInfo.php");
class usermodel extends CI_Model {
	
	function __construct() {
		parent::__construct ();
		$this->load->library('session'); 
		$this->load->library('email'); 
		$this->load->database();
	}
	function testuser($user,$pwd)
	{
		$query = $this->db->query('SELECT * FROM usertable where name="'.$user.'" and password="'.$pwd.'"');

		foreach ($query->result() as $row)
		{
			$tmpInfo = new userInfo();
			$tmpInfo->username = $row->name;
			$tmpInfo->password = $row->password;
			$tmpInfo->type =$row->type;
			$tmpInfo->id=$row->uid;
		    return $tmpInfo;
		}

		return null;
				
	}
	function getuserbytype($type)
	{
		$query = $this->db->query('SELECT * FROM usertable where type=?',array($type));
		return $query->result_array();
	}
	function getuserbylesstype($type)
	{
		$query = $this->db->query('SELECT * FROM usertable where type<?',array($type));
		return $query->result_array();
	}
	function getuserbyid($id)
	{
		$query = $this->db->query('SELECT * FROM usertable where uid=?',array($id));
		$array = $query->result_array();
		foreach($array as $row)
		{
			return $row;
		}
		return null;
	}
	function getuserinfo()
	{
		return $this->db->count_all('usertable');  
	}
	function getuserlist($start,$size)
	{
		$sql="select * from usertable limit ?,?";
		$query = $this->db->query($sql, array($start,$size));
		return $query;
	}
	function getuserall()
	{
		$sql="select * from usertable";
		$query = $this->db->query($sql);
		return $query;
	}
	function getuserinfodetail($id)
	{
		$sql="select * from usertable where uid=?";
		$query = $this->db->query($sql, array($id));
		return $query->result_array();
	}
	function checkuser($username)
	{
		$sql="select * from usertable where name=?";
		$query = $this->db->query($sql, array($username));
		return $query->num_rows();
	}
	function saveuserinfo($username,$password
			,$grade,$person,$post
			,$age,$phone,$email,$pic)
	{
	    $filename="";
		if($pic['size']>0 && $pic['name']!=""){
            $date1=date("YmdHis");
			$ext=explode(".",$pic['name']);
			$filename="template/upfile/".$date1.".".$ext[1];
			if (!move_uploaded_file($pic['tmp_name'],$filename)) {
			
					echo "<script>alert('复制文件失败');history.back()</script>";

					exit;
			}
		}
		$sql = "insert into usertable values(?,?,?,?,?,?,?,?,?,?)"; 
		$this->db->query($sql, array(null,$username,$password
			,$grade,$person,$post
			,$age,$phone,$email,$filename));		
		
		return $this->db->affected_rows();
				
	}
	function updateuserinfo($uid,$password
			,$grade,$person,$post
			,$age,$phone,$email,$pic)
	{
	   
	  $filename="";
		if($pic['size']>0 && $pic['name']!=""){
            $date1=date("YmdHis");
			$ext=explode(".",$pic['name']);
			$filename="template/upfile/".$date1.".".$ext[1];
			if (!move_uploaded_file($pic['tmp_name'],$filename)) {
			
					echo "<script>alert('复制文件失败');history.back()</script>";

					exit;
			}
		}
		$file=$filename ? ",pic=?" :"";
		$pw=$password?"password=?,":"";
		if($pw){
		 $da=array($password,$grade,$person,$post,$age,$phone,$email);
		}else{
		 $da=array($grade,$person,$post,$age,$phone,$email);
		}
		
		if($file){
		   array_push($da,$filename,$uid);
		 }else{
            array_push($da,$uid);
		 }
		//print_r($da);
		$sql = "update usertable set ".$pw."type=?,person=?,post=?,age=?,phone=?,email=?".$file." where uid=?"; 
		//die($sql);
		$this->db->query($sql,$da); 
		return $this->db->affected_rows();
				
	}
	function delete($id)
	{
		$sql="delete from usertable where uid=?";
		$query = $this->db->query($sql, array($id));
		return $this->db->affected_rows();
	}
	function sendmail($email,$title,$message)
	{
         $config['protocol'] = 'smtp';

		$config['smtp_host'] = '';
		
		$config['smtp_user'] = '';
		
		$config['smtp_pass'] = '';
				
		$config['smtp_port'] = 25;
		
		$config['wordwrap'] = TRUE;
		
		$config['mailtype'] = 'html';
		
		$config['charset'] = 'utf-8';
		
		$config['priority'] = 1;
		
		$config['validate'] = TRUE;
		
		$this->email->initialize($config);
		
		$email_from = $config['smtp_user'];
		
		$email_nick = "system mail";
		
		$email_to = $email;
		
		$email_subject = $title;
		
		$email_message = $message;
		
		$this->email->from($email_from, $email_nick);
		
		$this->email->to($email_to);
		
		$this->email->subject($email_subject);
		
		$this->email->message($email_message);	
		
		if($this->email->send()){		
		  return true;
		}else{
		   return false;
		} 
	}
}

?>